package cn.com.nou.common;

import cn.com.nou.common.constant.NouConstants;
import cn.com.nou.common.utils.CasUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Controller 基础类
 * @author lixuefeng
 */
public class BaseController {

    /**
     * 获取用户Id
     *
     * @return 用户Id
     */
//    protected String getUser() {
//        ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = attributes.getRequest();
//        String user = CasUtils.getUserId(request);
//        return user;
//    }

    protected String getAuthorization() {
        ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String auth = request.getHeader(NouConstants.ANTHORIZATION_HEADER);
        if (auth == null) {
            String token = request.getParameter(NouConstants.TOKEN_FIELD);
            if (token != null) {
                auth = String.format("%s %s", NouConstants.BEARER_TOKEN_TYPE, token);
            }
        }
        return auth;
    }
}
